package com.example.easypoi.util;


import com.example.easypoi.model.District;

import javax.script.*;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class scriptDemo {

    public static void main(String[] args) throws IOException, ScriptException, InterruptedException {
        Script();
    }


    public static List<District> Script() throws FileNotFoundException, ScriptException, InterruptedException {

        /*获取执行JavaScript的执行引擎*/
        ScriptEngine engine = new ScriptEngineManager().getEngineByName("javascript");
        /*为文件注入全局变量*/
        Bindings bindings = engine.createBindings();
        /*设置绑定参数的作用域*/
        engine.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
        /**获取文件路径 */
        String path = System.getProperty("user.dir") + "/src/main/resources/static/js/region.js";
        // 获得js文件
        engine.eval(new FileReader(path));
        System.out.println(bindings.get("regions"));
        // 定义得js 函数名称
        Map<String, Object> map = (Map) bindings.get("regions");
        List<District> list = new ArrayList<>();
        for (Map.Entry entry : map.entrySet()) {
            District district = new District();
            Map<String, Object> tempmap = (Map<String, Object>) entry.getValue();
            district.setValue(Integer.parseInt(String.valueOf(tempmap.get("value"))));
            district.setCity(String.valueOf(tempmap.get("label")));
            district.setCode(Integer.parseInt(String.valueOf(tempmap.get("value")))); //省 code
            district.setLevel(1);
            list.add(district);
            List<District> list1 = new ArrayList<>();
            //子集 市
            Map<String, Object> chil = (Map<String, Object>) tempmap.get("children");
            for (Map.Entry ent : chil.entrySet()) {
                Map<String, Object> ts = (Map<String, Object>) ent.getValue();
                District district1 = new District();
                district1.setCity(String.valueOf(ts.get("label")));
                district1.setValue(Integer.parseInt(String.valueOf(ts.get("value"))));
                district1.setCode(district.getValue());
                district1.setLevel(2);
                list.add(district1);
//                district.setChildren(list1);
                //子集 下的子集 区、县
                List<District> list2 = new ArrayList<>();
                Map<String, Object> ch = (Map<String, Object>) ts.get("children");
                for (Map.Entry sss1 : ch.entrySet()) {
                    Map<String, Object> t1 = (Map<String, Object>) sss1.getValue();
                    District dist = new District();
                    dist.setCity(String.valueOf(t1.get("label")));
                    dist.setValue(Integer.parseInt(String.valueOf(t1.get("value"))));
                    dist.setCode(district1.getValue());
                    dist.setLevel(3);
                    list.add(dist);
//                    district1.setChildren(list2);
                }
            }
        }

        System.out.println("======================================================================");
        System.out.println(list.toString());
        return list;
    }


//    public static void test2() throws IOException {
//        File file = new File("E:\\万息科技课程\\0007\\easypoi\\src\\main\\resources\\static\\js\\region.js");
//        BufferedReader br = new BufferedReader(new FileReader(file));
//        String st;
//        while ((st = br.readLine()) != null) {
////            System.out.println(st);
//            String[] split = st.split("=");
//            List<String> list = Arrays.asList(split[0]);
//
//            System.out.println(list.get(0));
//        }
//    }


}



